package com.ouc.service.impl;

import com.ouc.entity.Frame;
import com.ouc.entity.Order;
import com.ouc.mapper.FrameMapper;
import com.ouc.mapper.OrderMapper;
import com.ouc.service.OrderService;
import com.ouc.vo.OutOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Name: OrderServiceImpl
 * @Description:
 * @Author: Mr.Tong
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private FrameMapper frameMapper;

    @Override
    public Boolean insert(Order order) {
        return orderMapper.insert(order);
    }

    @Override
    public List<OutOrder> getAllOrdersByPaging(Integer userId) {
        //因为数据库中并没有存储totalprice，所以需要进行计算  因为线的价格在数据库中存储，无论怎样在计算总价格的时候总是要
        //回一趟数据库，所以不如直接在数据库中获取每一个产品的单价，而不需要使用代码重新计算（代码重新计算仍然需要从数据库中取出线的价格）
        List<OutOrder> allOrdersByUserId = orderMapper.getAllOrdersByUserId(userId);
        for (int i = 0; i < allOrdersByUserId.size(); i++) {
            double totalPrice = 0.0;
            for (int j = 0; j < allOrdersByUserId.get(i).getOrderItems().size(); j++) {
                String frameCode = allOrdersByUserId.get(i).getOrderItems().get(j).getFrame().getFrameCode();
                Frame frameByFrameCode = frameMapper.getFrameByFrameCode(frameCode);
                //计算房前条目的单价
                double unitPrice = frameByFrameCode.getCost() + frameByFrameCode.getProcessCost();
                totalPrice = unitPrice * (allOrdersByUserId.get(i).getOrderItems().get(j).getQuantity());
            }
            allOrdersByUserId.get(i).setTotalPrice(totalPrice);
        }
        return allOrdersByUserId;
    }

    @Override
    public List<OutOrder> getAllOrders(Integer userId) {
        return orderMapper.getAllOrders(userId);
    }
}
